package com.chuangjiangx.member.manager.basic.web.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response {

    private boolean success;
    private String err_code;
    private String err_msg;

    private Object data;

    public Response() {
        this.success = false;
    }

    public Response(boolean success) {
        this.success = success;
    }

    public Response(boolean success, String err_code, String err_msg) {
        this.success = success;
        this.err_code = err_code;
        this.err_msg = err_msg;
    }

    public void putData(String dataName, Object dataObj) {
        if (data == null || !(data instanceof Map)) {
            data = new HashMap<>();
        }
        ((Map) data).put(dataName, dataObj);
    }

    public Object getData(String dataName) {
        if (data == null || !(data instanceof Map)) {
            return null;
        }

        return ((Map) data).get(dataName);
    }
}
